﻿using FB.Mvc.Controllers;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wf5.Engine.Business;

namespace FCDB.Web.Controllers
{
    [FB.Membership.Annotations.AuthorizeOnly]
    public class MyProcessInstanceController : BaseController
    {
        //
        // GET: /MyProcessInstance/

        private WfProcessInstanceRepository Repository = new WfProcessInstanceRepository();
        private WfProcessRepository ProcessRepository = new WfProcessRepository();
        private IWorkflowService service = new WorkflowService();
        private WfActivityInstanceTasksRepository ActiveInstanceTaskRepository = new WfActivityInstanceTasksRepository();
          
        public ActionResult Index()
        {
             if (Request.IsAjaxRequest())
             {
                    int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfProcessInstanceEx> list = Repository.GetMyProcessInstance(this.UserID, skip, take, out total, dtStart, dtEnd, kewyword, ProcessGuid);
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json; 
                 
             }
             WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
             IList<WfProcess> processList = WFRespository.GetAllProcess();
             ViewBag.ProcessList = processList;
            return View();
        }


        public ActionResult ViewProccessInstance()
        {
            if (string.IsNullOrEmpty(Request["AppInstanceID"])) return Content("没有存在该表单");
            string AppInstanceID = Request["AppInstanceID"];
            string AppName = Request["AppName"];
            string ProcessGUID = Request["ProcessGUID"];
            WfProcessInstance instance =  service.GetProcessInstance(int.Parse(AppInstanceID)); 
            WfProcess process = ProcessRepository.GetById(instance.ProcessGUID);
            string url = string.Format("~/{0}?ProcessGuid={1}&AppInstanceID={2}&&ViewDetail=ViewDetail", process.PageUrl, process.ProcessGUID, instance.AppInstanceID);
            return Redirect(url);

        }

        public ActionResult DeleteMyProcessInstance()
        {

            if (string.IsNullOrEmpty(Request["AppInstanceID"])) return Content("没有存在该表单");
            string AppInstanceID = Request["AppInstanceID"];
            bool flag = Repository.DeleteProcessInstance(long.Parse(AppInstanceID));
            RtnMsg result = new RtnMsg()
            {
                Status = 1
            };
            if (flag)
                result.Status = 1;

            else
                result.Status = 2;
            return Json(result);
        }

    }
}
